﻿using Ruici.BLL;
using Ruici.Web.Services.Model;
using RuiciWebSite.Common;
using RuiciWebSite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RuiciWebSite.Controllers
{
    public class MedicalWikiController : Controller
    {
        DocumentBll document = new DocumentBll();
        // GET: MedicalWiki
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取疾病讲解文档
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetSaySickDocuments(PageInfoViewModel pageInfo)
        {
            int iTotalCount = 0;
            pageInfo.curPageRecord = document.GetArticleList("video", 0, pageInfo.pageSize, pageInfo.pageCurrent, pageInfo.keyWord , out iTotalCount);
            pageInfo.totalCount = iTotalCount;
            pageInfo.pageCount = (iTotalCount + pageInfo.pageSize -1)/pageInfo.pageSize;
            pageInfo.ImgUrl = RuiciCommoms._sImgPath;
            if (pageInfo!=null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有找到相关文章"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取疾病相关文章
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetDiseaseWikiDocuments(PageInfoViewModel pageInfo)
        {
            int iTotalCount = 0;
            pageInfo.curPageRecord = document.GetArticleList("disease", 0, pageInfo.pageSize, pageInfo.pageCurrent, pageInfo.keyWord, out iTotalCount);
            pageInfo.totalCount = iTotalCount;
            pageInfo.pageCount = (iTotalCount + pageInfo.pageSize - 1) / pageInfo.pageSize;
            pageInfo.ImgUrl = RuiciCommoms._sImgPath;
            if (pageInfo != null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有找到相关文章"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取分享案例
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetSharingCaseDocuments(PageInfoViewModel pageInfo)
        {
            int iTotalCount = 0;
            
            pageInfo.curPageRecord = document.GetArticleList("case", 0, pageInfo.pageSize, pageInfo.pageCurrent, pageInfo.keyWord, out iTotalCount);
            pageInfo.totalCount = iTotalCount;
            pageInfo.pageCount = (iTotalCount + pageInfo.pageSize - 1) / pageInfo.pageSize;
            pageInfo.ImgUrl = RuiciCommoms._sImgPath;
            if (pageInfo != null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有找到相关文章"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取指南共识文档
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetGuideConsensusDocuments(PageInfoViewModel pageInfo)
        {
            int iTotalCount = 0;
            int categoryId = 0;
            List<int> categoryIdList = new List<int>();
            if (pageInfo.category != null)
            { 
                for(int i=0;i<pageInfo.category.Length;i++)
                {
                    categoryIdList.Add(pageInfo.category[i].id);
                }
                                
                 pageInfo.curPageRecord = document.GetArticleList("guide", categoryIdList, pageInfo.pageSize, pageInfo.pageCurrent, pageInfo.keyWord, out iTotalCount);
            }
            else
            {
                pageInfo.curPageRecord = document.GetArticleList("guide", 0, pageInfo.pageSize, pageInfo.pageCurrent, pageInfo.keyWord, out iTotalCount);
            }
            
            pageInfo.totalCount = iTotalCount;
            pageInfo.pageCount = (iTotalCount + pageInfo.pageSize - 1) / pageInfo.pageSize;
            pageInfo.ImgUrl = RuiciCommoms._sImgPath;
            if (pageInfo != null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有找到相关文章"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取类别信息
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetCategories(PageInfoViewModel pageInfo)
        {
            Dictionary<string, List<ArticleCategoryResponse>> articleCategory = document.GetCategoryList(pageInfo.categoryType, 0);
            List<ArticleCategory> ArticleCategoryList = new List<ArticleCategory>();
            
            foreach (string parentCategoryName in articleCategory.Keys)
            {
                ArticleCategory category = new ArticleCategory();
                List<ArticleCategoryResponse> CategoryList = new List<ArticleCategoryResponse>();
                category.parent_Category_name = parentCategoryName;
                category.sub_category_list = new List<subArticleCategory>();
                List<ArticleCategoryResponse> subCategoryList = new List<ArticleCategoryResponse>();
                articleCategory.TryGetValue(parentCategoryName, out subCategoryList);
                foreach(ArticleCategoryResponse subCategory in subCategoryList)
                {
                    subArticleCategory subArtCat = new subArticleCategory();
                    subArtCat.id = subCategory.id;
                    subArtCat.channel_id = subCategory.channel_id;
                    subArtCat.title = subCategory.title;
                    subArtCat.call_index = subCategory.call_index;
                    subArtCat.parent_id = subCategory.parent_id;
                    subArtCat.class_list = subCategory.class_list;
                    subArtCat.class_layer = subCategory.class_layer;
                    subArtCat.sort_id = subCategory.sort_id;
                    subArtCat.link_url = subCategory.link_url;
                    subArtCat.img_url = subCategory.img_url;
                    subArtCat.content = subCategory.content;
                    subArtCat.seo_title = subCategory.seo_title;
                    subArtCat.seo_keywords = subCategory.seo_keywords;
                    subArtCat.seo_description = subCategory.seo_description;
                    subArtCat.isSelected = false;
                    category.sub_category_list.Add(subArtCat);
                }
                ArticleCategoryList.Add(category);
            }

            if (ArticleCategoryList != null)
                return Json(ArticleCategoryList, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有找到相关文章"), JsonRequestBehavior.AllowGet);
        }
    }
}